package cn.qxnekoo.chain;

import cn.qxnekoo.chain.service.*;
import cn.qxnekoo.chain.service.impl.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

/**
 * 职责链设计模式
 */
@SpringBootApplication
@RestController
public class ChainApplication {


	@Autowired
	IWomen women;

	public static void main(String[] args) {
		SpringApplication.run(ChainApplication.class, args);
	}

	@RequestMapping("/test/{id}")
	public String test(@PathVariable("id") Integer level) {

		Chain chain = initChain();
		//使用代理执行women中的excute 方法，条件是当level 和链Chain中的值相同时
		AbstractRequest request = new AbstractRequest(level, new ProxyFactory(women).getProxyInstance());
		// 给目标对象，创建代理对象
		//没有执行情况时
		if (!chain.handleRequest(request)) {
			System.out.println("没有找到合适的处理级别，可进行下一步处理");
		}
		return null;
	}

	/**
	 * 初始化链规则
	 * @return
	 */
	static Chain initChain() {
		Chain chain = new Chain(1);
		Chain chain1 = new Chain(2);
		Chain chain2 = new Chain(3);
		Chain chain3 = new Chain(4);
		chain.setNextHandler(chain1);
		chain1.setNextHandler(chain2);
		chain2.setNextHandler(chain3);
		return chain;
	}
}
